Page({
  data:{
    wifiInfo: null,
    loading:false,
    errorMessage: ''
  },
  onLoad(query) {
    //todo:get code from app global
    let code  = "code" in query ? query.code : 'test002'; // 从全局拿商家ID
    console.info("[index] use merchantMaterial =", code);

    //get wificore
    my.request({
      url: "https://rongtaplink.top/taplink/api/getWifiCore",
      method: "GET",
      dataType: "json",
      data: { merchantMaterial: code },
      success: (res) => {
        console.log("logRes:"+ JSON.stringify(res.data));
        var account = res.data.result.account;
        var pwd = res.data.result.pwd;
        if(!account || !pwd){
          return;
        }
        this.setData({ wifiInfo: { account, pwd } });
        //request请求为异步请求，将连接wifi逻辑放入request中
             // 1. 先初始化 Wi-Fi 模块
        my.startWifi({
          success: function(res) {
            console.log(account);
            console.log(pwd);
            // 2. 连接 Wi-Fi
            my.connectWifi({
              SSID: account, 
              password: pwd,
              success: function(res) {
                console.log(res);
              }
            });
          }
        });
        // 3. 监听连接上 Wi-Fi
        my.onWifiConnected(function(res) {
          console.log(res);
        })
      },
      fail: () => {
        console.log("GetWifiCore error");
      }
    });
  },
    
  onReady() {
    // 页面加载完成
  },
  onShow() {
    // 页面显示
  },
  onHide() {
    // 页面隐藏
  },
  onUnload() {
    // 页面被关闭
  },
  onTitleClick() {
    // 标题被点击
  },
  onPullDownRefresh() {
    // 页面被下拉
  },
  onReachBottom() {
    // 页面被拉到底部
  },
  onShareAppMessage() {
    // 返回自定义分享信息
    return {
      title: 'My App',
      desc: 'My App description',
      path: 'pages/index/index',
    };
  },
});
